// let Color3 = require('./core/color3');
// let Vec2 = require('./core/vec2');

// let Color3 = window.dd.Color3;
// let Vec2 = window.dd.Vec2;

let settings = {

    // webgl绘制区域大小
    SCREEN_WIDTH: 480,
    SCREEN_HEIGHT: 800,

    dev: false, // true为开发环境, false为发布环境
    // defaultPath:'/Users/dai/work/ccc/zmeditor',
    // defaultBattery:'/Users/dai/work/ccc/zmeditor/assets/battery.png',
    // defaultDecoPath:'/Users/dai/work/ccc/zmeditor/decos',

    offscreenCanvas: null,

    defaultPath: '~/',
    // defaultBattery:'/Users/dai/work/ccc/zmeditor/assets/battery.png',
    defaultDecoPath: '~/',

    SelNodeWidth: 20,
    SelNodeHeight: 20,

    SC_IDX_GAP: 25,

    // zorder
    Z_SelNode: 10,  // selnode 的 zorder
    Z_BkpNode: 9,  // bkp的zorder
    Z_Bg: -1,  // 背景
    Z_SafeArea: -2,  // 安全区域

    /**
     *  chain 上的 zorder
     *  1. selnode   10   应该最高
     *  2. bknode    9
     *  3. subChain  可能几种:
     *     case1: 高zorder 4
     *     case2: 低zorder 0
     *
     *     chain的开始zorder有关:
     *     case1: 开始为高zorder:
     *     case2：开始为低zorder:
     *
     */

    Z_Chain_High: 4,
    Z_Chain_Low: 1,

    Z_Chain_Mid: 3, // 桥或者洞

    Z_Battery: 5,
    Z_SelectedFlag: 10000,

    // 桥/洞/碰撞在 chain上的zIndex
    Z_Hole: 1,
    Z_Collision: 2,
    Z_Bridge: 3,


    Head_Width: 30,
    Head_Height: 30,

    // bkp大小和颜色
    BkpWidth: 15,
    BkpHeight: 15,


    // bkp
    CHalfWidth: 28,

    // battery 的名字
    default_battery_1: '$$battery_1_$$', // 用作主发射器
    default_battery_2: '$$battery_2_$$',

    // 发射器的一些位置
    BtyNarrow: 20,
    BtyDefaultMidX: 480,
    BtyDefaultMidY: 800,
    BtyTopY: 1260,
    BtyBottonY: 340,
    BtyLeftX: 206,
    BtyRightX: 762,

    init() {
        // 设计一套颜色
        /**
         * 1.  selnode  普通颜色为 WHITE, 选中为红色
         * 2.
         */
        // this.Color_SelNode_Normal = cc.Color.BLACK;
        // this.Color_SelNode_Selected = cc.Color.RED;
        // this.Color_Chain_Normal = cc.Color.WHITE;
        // this.Color_Chain_Selected = cc.Color.YELLOW;
        // this.Color_Chain_Head = cc.Color.MAGENTA;
        // this.BkpColor = cc.Color.GREEN;
    },

    selectDir() {
        let dialog = window.electron.remote.dialog;
        let lstPath = dialog.showOpenDialogSync({
            properties: ['openDirectory'],
            defaultPath: settings.defaultPath,
        });
        if (lstPath) {
            return lstPath[0];
        }
        return null;
    },

    selectFile() {
        let dialog = window.electron.remote.dialog;
        let lstPath = dialog.showOpenDialogSync({
            properties: ['openFile'],
            defaultPath: settings.defaultPath,
        });
        if (lstPath) {
            return lstPath[0];
        }
        return null;
    },

    getSaveFilePath(extNameWithPoint) {
        let dialog = window.electron.remote.dialog;
        let finalPath = dialog.showSaveDialogSync({
            defaultPath: settings.defaultPath,
            filters: [{
                name: extNameWithPoint,
                extensions: [extNameWithPoint]
            }],
        });
        return finalPath;
    },

    showConfirmDialog(msg) {
        let dialog = window.electron.remote.dialog;
        let retIdx = dialog.showMessageBoxSync({
            title: '提示',
            message: msg,
            buttons: ['确定', '取消'],
            defaultId: 0,
        });
        return retIdx === 0;
    },

    alert(msg) {
        let dialog = window.electron.remote.dialog;
        let retIdx = dialog.showMessageBoxSync({
            title: '提示',
            message: msg,
            buttons: ['确定'],
            defaultId: 0,
        });
        return retIdx === 0;
    },

    // 需要优化下性能
    calVerts(lstArr) {
        let vertices = [];
        if (lstArr.length < 2) {
            console.log('selnode数据不够');
            return vertices;
        }
        let len = lstArr.length;
        let idx_gap = settings.SC_IDX_GAP;
        let curIdx = 0;
        while (curIdx < len - 1) {
            let cur = lstArr[curIdx];
            let nextIdx = curIdx + idx_gap;
            if (nextIdx > len - 1) {
                nextIdx = len - 1;
            }
            let next = lstArr[nextIdx];
            let [v0, v1] = this._calVert(cur.pos, next.pos);
            vertices.push(v0.x, v0.y, v1.x, v1.y);
            curIdx = nextIdx;
        }

        // 最后一个
        let cur = lstArr[len - 2];
        let next = lstArr[len - 1];
        let [v0, v1] = this._calVert(cur.pos, next.pos);
        vertices.push(v0.x, v0.y, v1.x, v1.y);
        // console.log('vertices.size:',vertices.length);
        return vertices;
    },

    _getPositiveLeft(vec2){
        return cc.v2(-vec2.y,vec2.x);
    },

    _calVert(A, B) {
        const HalfR = settings.CHalfWidth;
        let AB = cc.v2(B.x - A.x,B.y - A.y);
        let AD = this._getPositiveLeft(AB);
        AD.normalize();
        let D =  cc.v2(A.x + AD.x * HalfR, A.y + AD.y * HalfR);
        let C =  cc.v2(A.x - AD.x * HalfR, A.y - AD.y * HalfR);

        // let F = new Vec2(B.x + AD.x * HalfR,B.y + AD.y * HalfR);
        // let E = new Vec2(B.x - AD.x * HalfR,B.y - AD.y * HalfR);
        //
        // // let v0 = C;
        // // let v1 = D;
        // // let v2 = E;
        // // let v3 = F;
        // return [C,D,E,F];
        return [C, D];
    },


    // clamp(value, min, max) {
    //     return Math.min(Math.max(value, min), max);
    // }
};

window.settings = settings;
module.exports = settings;
